package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 创建垂直方向的线性布局作为根布局
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));


        ImageView flagImage = new ImageView(this);
        flagImage.setImageResource(R.drawable.china_flag);
        LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(300, 200);
        imgParams.topMargin = 50;
        imgParams.gravity = android.view.Gravity.CENTER_HORIZONTAL;
        flagImage.setLayoutParams(imgParams);


        TextView helloText = new TextView(this);
        helloText.setText("你好，世界！");
        helloText.setTextSize(24);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        textParams.topMargin = 20;
        textParams.gravity = android.view.Gravity.CENTER_HORIZONTAL;
        helloText.setLayoutParams(textParams);

        // 中文切换按钮
        Button chinaBtn = new Button(this);
        chinaBtn.setText("中文");
        LinearLayout.LayoutParams chinaBtnParams = new LinearLayout.LayoutParams(150, ViewGroup.LayoutParams.WRAP_CONTENT);
        chinaBtnParams.topMargin = 30;
        chinaBtnParams.gravity = android.view.Gravity.CENTER_HORIZONTAL;
        chinaBtn.setLayoutParams(chinaBtnParams);
        chinaBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flagImage.setImageResource(R.drawable.china_flag);
                helloText.setText("你好，世界！");
            }
        });

        // 英文切换按钮
        Button englishBtn = new Button(this);
        englishBtn.setText("英文");
        LinearLayout.LayoutParams englishBtnParams = new LinearLayout.LayoutParams(150, ViewGroup.LayoutParams.WRAP_CONTENT);
        englishBtnParams.topMargin = 10;
        englishBtnParams.gravity = android.view.Gravity.CENTER_HORIZONTAL;
        englishBtn.setLayoutParams(englishBtnParams);
        englishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flagImage.setImageResource(R.drawable.england_flag);
                helloText.setText("Hello, World!");
            }
        });

        // 日文切换按钮
        Button japanBtn = new Button(this);
        japanBtn.setText("日文");
        LinearLayout.LayoutParams japanBtnParams = new LinearLayout.LayoutParams(150, ViewGroup.LayoutParams.WRAP_CONTENT);
        japanBtnParams.topMargin = 10;
        japanBtnParams.gravity = android.view.Gravity.CENTER_HORIZONTAL;
        japanBtn.setLayoutParams(japanBtnParams);
        japanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flagImage.setImageResource(R.drawable.japan_flag);
                helloText.setText("こんにちは、世界！");
            }
        });


        linearLayout.addView(flagImage);
        linearLayout.addView(helloText);
        linearLayout.addView(chinaBtn);
        linearLayout.addView(englishBtn);
        linearLayout.addView(japanBtn);

        // 设置根布局
        setContentView(linearLayout);
    }
}